import * as actionTypes from './actionTypes';

export default function reducer(state = [], action) {
    switch(action.type) {
        case actionTypes.ADD_TODO: {
            return [
                ...state,
                {
                    id: action.id,
                    text: action.text,
                    completed: action.completed
                }
            ];
        }
        case actionTypes.TOGGLE_TODO: {
            return state.map((item, index) => {
            	if(item.id === action.id) {
            	    return {...item, completed: !item.completed};
                }
                return item;
            });
        }
        case actionTypes.REMOVE_TODO: {
            return state.filter((item, index) => {
            	return item.id !== action.id;
            });
        }
        default:
            return state;
    }
}